<?php
namespace app\admin\model;
use my\Security;
use think\Db;
use think\Model;

class FinanceModel extends Model
{
	 public function __construct()
    {
        parent::__construct();

    }
    /**
     * 用户资金流水
     */
    public function userFlowList($page,$limit,$data){
        //时间
//        if(!empty($data['time_type']) && $data['time_type'] == 1){
            //记录时间
            if (!empty($data['start_time']) && empty($data['end_time'])) {
                $where['a.time'] = ['>=',strtotime(rtrim($data['start_time']))];
            }elseif(empty($data['start_time']) && !empty($data['end_time'])){
                $where['a.time'] = ['<=',strtotime(rtrim($data['end_time'])." 23:59:59")];
            }elseif(!empty($data['start_time']) && !empty($data['end_time'])){
                $where['a.time'] = ['between',[strtotime(rtrim($data['start_time'])),strtotime(rtrim($data['end_time'])." 23:59:59")]];
            }
//        }
//        elseif(!empty($data['time_type']) && $data['time_type'] == 2){
//            //注册时间
//            if (!empty($data['start_time']) && empty($data['end_time'])) {
//                $where['b.register_time'] = ['>=',strtotime(rtrim($data['start_time']))];
//            }elseif(empty($data['start_time']) && !empty($data['end_time'])){
//                $where['b.register_time'] = ['<=',strtotime(rtrim($data['end_time'])." 23:59:59")];
//            }elseif(!empty($data['start_time']) && !empty($data['end_time'])){
//                $where['b.register_time'] = ['between',[strtotime(rtrim($data['start_time'])),strtotime(rtrim($data['end_time'])." 23:59:59")]];
//            }
//        }
	     //手机号码
	     if(!empty($data['user_phone'])){
             $where['b.user_phone']   = Security::encrypt($data['user_phone']);
         }
        //订单号
        if(!empty($data['order_sn'])){
            $where['out_trade_no']   = trim($data['order_sn'],'');
        }
//        //用户角色
//        if(isset($data['role_type']) && $data['role_type'] != ''){
//            $where['b.is_fr']   = $data['role_type'];
//        }
        //金额类型
        if(!empty($data['money_statement'])){
            $where['a.money_statement']   = $data['money_statement'];
        }
        if(!empty($data['uid'])) {
            $where['a.uid']   = $data['uid'];
        }
        $where['b.false_status']= 0;
        $where['b.is_del']      = 1;
        //字段
        $field  = "a.*,b.user_phone,b.is_fr,b.register_time,w.wallet_money";
        //分页
        $start  = ($page - 1) * $limit;
        $count  = Db::name('user_recharge')->alias('a')->join('user b','a.uid = b.uid','left')->join('user_wallet w','a.uid = w.uid', 'left')->where($where)->count();
        $list = Db::name('user_recharge')->alias('a')
            ->join('user b','a.uid = b.uid','left')
            ->join('user_wallet w','a.uid = w.uid', 'left')
            ->field($field)->where($where)->limit($start, $limit)->order('a.money_id','desc')->select();

        foreach ($list as $key=>$value){
            $list[$key]['user_phone']       = Security::decrypt($value['user_phone']);
//            $list[$key]['register_time']    = !empty($value['register_time'])?date('Y-m-d H:i:s',$value['register_time']):'';
            $list[$key]['time']             = !empty($value['time'])?date('Y-m-d H:i:s',$value['time']):'';
            $list[$key]['role_type']        = !empty($value['is_fr'])?'<span style="color: green">顺风车车主</span>':'用户';
            $list[$key]['flow_money']            = sprintf("%.2f",($value['money']+$value['return_money']+$value['gifts_money']) / 100);
            $list[$key]['return_money']     = sprintf("%.2f",$value['return_money'] / 100); //不可提现金额
            $list[$key]['money']     = sprintf("%.2f",$value['money'] / 100); //可提现
            $list[$key]['gifts_money']     = sprintf("%.2f",$value['gifts_money'] / 100); //赠额
            $list[$key]['money_statement']  = empty($value['money_statement'])?'-':user_flow_type($value['money_statement']);
//            $list[$key]['wallet_money']     = sprintf("%.2f",$value['wallet_money'] / 100); //用户当前余额
        }

        return array(
            'code'  => 0,
            'msg'   => '',
            'count' => $count,
            'data'  => $list
        );
    }
    /**
     * 司机资金流水
     */
    public function driverFlowList($page,$limit,$data){
        //时间
        if(!empty($data['time_type']) && $data['time_type'] == 1){
            //记录时间
            if (!empty($data['start_time']) && empty($data['end_time'])) {
                $where['a.time'] = ['>=',strtotime(rtrim($data['start_time']))];
            }elseif(empty($data['start_time']) && !empty($data['end_time'])){
                $where['a.time'] = ['<=',strtotime(rtrim($data['end_time'])." 23:59:59")];
            }elseif(!empty($data['start_time']) && !empty($data['end_time'])){
                $where['a.time'] = ['between',[strtotime(rtrim($data['start_time'])),strtotime(rtrim($data['end_time'])." 23:59:59")]];
            }
        }elseif(!empty($data['time_type']) && $data['time_type'] == 2){
            //注册时间
            if (!empty($data['start_time']) && empty($data['end_time'])) {
                $where['c.register_time'] = ['>=',strtotime(rtrim($data['start_time']))];
            }elseif(empty($data['start_time']) && !empty($data['end_time'])){
                $where['c.register_time'] = ['<=',strtotime(rtrim($data['end_time'])." 23:59:59")];
            }elseif(!empty($data['start_time']) && !empty($data['end_time'])){
                $where['c.register_time'] = ['between',[strtotime(rtrim($data['start_time'])),strtotime(rtrim($data['end_time'])." 23:59:59")]];
            }
        }
        //手机号码
        if(!empty($data['driver_phone'])){
            $where['b.driver_phone']   = Security::encrypt($data['driver_phone']);
        }
        //用户角色
        if(isset($data['driver_type']) && $data['driver_type'] != ''){
            $where['b.driver_type']   = $data['driver_type'];
        }
        //金额类型
        if(!empty($data['record_type'])){
            $where['a.record_type']   = $data['record_type'];
        }
        //字段
        $field  = "a.*,b.driver_phone,b.driver_type,c.register_time";
        //分页
        $start  = ($page - 1) * $limit;
        $count  = Db::name('driver_wallet_record')->alias('a')->join('driver_base b','a.driver_id = b.driver_id','left')->join('driver_info c','b.driver_id = c.driver_id','left')->where($where)->count();
        $list   = Db::name('driver_wallet_record')->alias('a')->join('driver_base b','a.driver_id = b.driver_id','left')->join('driver_info c','b.driver_id = c.driver_id','left')->field($field)->where($where)->limit($start, $limit)->order('a.record_id','desc')->select();
        //echo Db::name('user_recharge')->alias('a')->getLastSql();
        foreach ($list as $key=>$value){
            $list[$key]['driver_phone']     = Security::decrypt($value['driver_phone']);
            $list[$key]['register_time']    = !empty($value['register_time'])?date('Y-m-d H:i:s',$value['register_time']):'';
            $list[$key]['time']             = !empty($value['time'])?date('Y-m-d H:i:s',$value['time']):'';
            $list[$key]['driver_type']      = $value['driver_type'] == 1 ? '<span style="color:green">快车</span>':'城际';
            $list[$key]['money']            = sprintf("%.2f",$value['money'] / 100);
            $list[$key]['record_type']      = empty($value['record_type'])?'-':driver_flow_type($value['record_type']);
        }
        return array(
            'code'  => 0,
            'msg'   => '',
            'count' => $count,
            'data'  => $list
        );
    }
    /*public function userMoneyList($start,$limit,$where){
    	return Db::name('user')->alias('u')->field('u.*,w.*')->where($where)->join("__USER_WALLET__ w "," u.uid = w.uid")->limit($start,$limit)->order('u.uid desc')->select();
    }*/
}
